package com.fg.cloud.factory.controller.agency;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.AgencyAccount;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.agency.AgencyAccountEntity;
import com.fg.cloud.manager.entry.agency.AgencyBillEntity;
import com.fg.cloud.manager.service.agency.AgencyAccountService;

/**
 * @Description:经销商账户管理
 * @author dingjiang
 * @date 2017年8月11日上午9:04:37
 */
@Controller
@RequestMapping("/agencyAccount")
public class AgencyAccountController extends BaseController {

	@Autowired
	private AgencyAccountService agencyAccountService;

	/**
	  * Description:查询账户详情
	  * @param request
	  * @param response
	  * @param id
	  * @author shaoqiping
	  * @date 2017年8月31日上午9:20:39
	*/
	@RequestMapping(value = "/find", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void find(HttpServletRequest request, HttpServletResponse response,AgencyAccountEntity agencyAccount){
		try {
			if(agencyAccount==null||LongUtils.isEmpty(agencyAccount.getAgeId())){
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择相应经销商");
				return;
			}
			agencyAccount.setFacId(getCurrentFactoryUser().getFacId());
			List<AgencyAccountEntity> account=agencyAccountService.findAccount(agencyAccount);
			if(account.size()>0){
				doResult(response, true, StatusCode.FINDSUCCESS, account, ResultType.FIND_SUCCESS);
			}else{
				doResult(response, false, StatusCode.FINDERROR, account, ResultType.FIND_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	/**
	  * Description:信用账户启用/禁用
	  * @param request
	  * @param response
	  * @param entity
	  * @author shaoqiping
	  * @date 2017年8月30日下午6:23:11
	*/
	@RequestMapping(value = "/openOrClose", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void update(HttpServletRequest request, HttpServletResponse response, AgencyAccountEntity entity) {
		try {
			// 判断账户ID不为空
			if (entity == null || LongUtils.isEmpty(entity.getAgeId()) || LongUtils.isEmpty(entity.getId())
					|| entity.getLimitStatus() == null) {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.INPUT_NULL);
				return;
			}
			// 判断账户类型是否为信用账户
			entity.setFacId(getCurrentFactoryUser().getFacId());
			AgencyAccountEntity temp = agencyAccountService.selectBycondition(entity);
			if (temp == null) {
				doResult(response, false, StatusCode.SAVEERROR, null, "账户不存在");
				return;
			}
			if (temp.getType() == AgencyAccount.CASH) {
				doResult(response, false, StatusCode.SAVEERROR, null, "现金账户无法开通/禁用");
				return;
			}
			setUpdateInfo(entity, request);
			int flag = agencyAccountService.update(entity);
			if (flag > 0) {
				if(entity.getLimitStatus()==1){
					doResult(response, true, StatusCode.SAVESUCCESS, null, "启用成功");
				}else{
					doResult(response, true, StatusCode.SAVESUCCESS, null, "禁用成功");
				}
			} else {
				if(entity.getLimitStatus()==1){
					doResult(response, false, StatusCode.SAVEERROR, null,"启用失败");
				}else{
					doResult(response, false, StatusCode.SAVEERROR, null,"禁用失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	
	/**
	  * Description:信用额度调整
	  * @param request
	  * @param response
	  * @param entity
	  * @param number
	  * @author shaoqiping
	  * @date 2017年8月30日下午6:23:39
	*/
	@RequestMapping(value = "/adjust", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void adjust(HttpServletRequest request, HttpServletResponse response, AgencyAccountEntity entity,
			BigDecimal number) {
		try {
			if (entity == null || LongUtils.isEmpty(entity.getAgeId()) || LongUtils.isEmpty(entity.getId())
					|| number == null) {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.INPUT_NULL);
				return;
			}
			if (number.compareTo(new BigDecimal(0)) <= 0) {
				doResult(response, false, StatusCode.SAVEERROR, null, "调整数必须为正数");
				return;
			}
			entity.setFacId(getCurrentFactoryUser().getFacId());
			AgencyAccountEntity temp = agencyAccountService.selectBycondition(entity);
			if (temp == null) {
				doResult(response, false, StatusCode.SAVEERROR, null, "账户不存在");
				return;
			}
			if (temp.getType() == AgencyAccount.CASH) {
				doResult(response, false, StatusCode.SAVEERROR, null, "账户为现金账户");
				return;
			}
			if (temp.getLimitStatus() == AgencyAccount.DISABLED) {
				doResult(response, false, StatusCode.SAVEERROR, null, "信用账户尚未开通");
				return;
			}
			if (number.compareTo(temp.getLimitMoney()) > 0) {
				entity.setLimitUsable(temp.getLimitUsable().add(number.subtract(temp.getLimitMoney())));
			}
			entity.setType(temp.getType());
			setUpdateInfo(entity, request);
			entity.setLimitMoney(number);
			boolean flag = agencyAccountService.modifyLimit(entity, number, temp.getLimitMoney(),getCurrentFactoryUser().getTrueName());
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:上调可用金额-信用还款
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @param number
	 * @author shaoqiping
	 * @date 2017年8月30日下午4:33:22
	 */
	@RequestMapping(value = "/repayment", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void repayment(HttpServletRequest request, HttpServletResponse response, AgencyAccountEntity entity,
			BigDecimal number,AgencyBillEntity billEntity) {
		try {
			if (entity == null || LongUtils.isEmpty(entity.getAgeId()) || LongUtils.isEmpty(entity.getId())
					|| number == null) {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.INPUT_NULL);
				return;
			}
			if (number.compareTo(new BigDecimal(0)) <= 0) {
				doResult(response, false, StatusCode.SAVEERROR, null, "调整数必须为正数");
				return;
			}
			entity.setFacId(getCurrentFactoryUser().getFacId());
			AgencyAccountEntity temp = agencyAccountService.selectBycondition(entity);
			if (temp == null) {
				doResult(response, false, StatusCode.SAVEERROR, null, "账户不存在");
				return;
			}
			if (temp.getType() == AgencyAccount.CASH) {
				doResult(response, false, StatusCode.SAVEERROR, null, "账户为现金账户");
				return;
			}
			if (temp.getLimitStatus() == AgencyAccount.DISABLED) {
				doResult(response, false, StatusCode.SAVEERROR, null, "信用账户尚未开通");
				return;
			}
			if (number.compareTo(temp.getLimitMoney().subtract(temp.getLimitUsable())) > 0) {
				doResult(response, false, StatusCode.SAVEERROR, null, "调整金额高于已用额度");
				return;
			}
			entity.setType(temp.getType());
			entity.setLimitUsable(temp.getLimitUsable().add(number));
			setUpdateInfo(entity, request);
			billEntity.setAdmin(getCurrentFactoryUser().getTrueName());
			boolean flag = agencyAccountService.modify(entity, number,billEntity);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
			e.printStackTrace();
		}
	}

	
	/**
	  * Description:厂商现金账户代理充值
	  * @param request
	  * @param response
	  * @param entity
	  * @param number
	  * @author shaoqiping
	  * @date 2017年8月31日下午3:23:27
	*/
	@RequestMapping(value = "/recharge", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void recharge(HttpServletRequest request, HttpServletResponse response, AgencyAccountEntity entity,
			BigDecimal number,AgencyBillEntity billEntity) {
		try {
			if(entity==null||LongUtils.isEmpty(entity.getAgeId())){
				doResult(response, false, StatusCode.SAVEERROR, null, "请选择相应经销商");
				return;
			}
			if(number==null||number.compareTo(new BigDecimal(0))<=0){
				doResult(response, false, StatusCode.SAVEERROR, null, "请填写正确的金额");
				return;
			}
			entity.setFacId(getCurrentFactoryUser().getFacId());
			entity.setType(AgencyAccount.CASH);
			AgencyAccountEntity temp = agencyAccountService.selectBycondition(entity);
			entity.setLimitUsable(temp.getLimitUsable().add(number));
			entity.setId(temp.getId());
			setUpdateInfo(entity, request);
			billEntity.setAdmin(getCurrentFactoryUser().getTrueName());
			boolean flag = agencyAccountService.modify(entity, number,billEntity);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
}
